<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input placeholder="输入机构名称/编号/负责人" v-model="dataForm.freeCondition" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-input placeholder="输入调查专员姓名/工号" v-model="dataForm.userNameOrRealName" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList(0)">
            <fa-icon class="fa-search" />查询</el-button>
          <el-button plain type="" @click="onReset">
            <fa-icon class="fa-refresh" />重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="top-panel__box">
        <el-form onsubmit="return false" size="small">
          <el-form-item>
            <el-button type="primary" @click="addInves" v-permission="'BEFORE_ORG_ADD'">
                <fa-icon class="el-icon-plus" />新增调查机构
            </el-button>
          </el-form-item>
        </el-form>
    </div>
    <el-row class="table-top">
      <el-table :border="true" :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in TEAM_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column fixed="right" class-name="oper-col" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" plain @click="showEditor(scope.row)" v-permission="'BEFORE_LENDING_ORG_AUDIT'">
                编辑调查专员
              </el-button>
              <el-button type="primary" size="mini" plain @click="deletPersonInfo(scope.row)" v-permission="'BEFORE_ORG_DELETE'">
                移除
              </el-button>
            </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-dialog title="新增机构" :visible.sync="editInves" width="30%" :close-on-click-modal="false">
      <edit-investigative v-if="editInves" :listValue="userValueList" @updataSuccess="getInvesData" @close="closeInvesChoose" @showType="showUser" />
    </el-dialog>
    <el-dialog title="编辑成员" :visible.sync="editUserPop" width="60%" :close-on-click-modal="false">
      <choose-people v-if="editUserPop" :valueList="userValueList" @updataSuccess="getUserData" @close="closeUserChoose" @clearList="clearList" />
    </el-dialog>
  </div>
</template>

<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { TEAM_HEAD_LIST } from '../js/contant.js'
import { common } from '@/utils/common'
import ChoosePeople from './ChoosePeople'
import EditInvestigative from './EditInvestigative'
export default {
  components: {ChoosePeople, EditInvestigative},
  data () {
    return {
      TEAM_HEAD_LIST,
      tableLoading: false,
      editUserPop: false,
      editInves: false,
      dataForm: {
        freeCondition: '',
        userNameOrRealName: '',
        pageNum: 0,
        pageSize: 10
      },
      dataList: [{}],
      userValueList: [],
      editorDialog: false,
      total: 0,
      editUser: {},
      uploadDialog: false
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.queryInvestigativeList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    queryShowList () {
      let { organizationId } = this.editUser
      api.getSettingData(organizationId).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          console.log(data)
          this.userValueList = [...data]
        }
      })
    },
    showEditor (row) {
      this.editUserPop = true
      this.editUser = {...row}
      this.queryShowList()
    },
    deletPersonInfo (row) {
      console.log(row)
      common.confirmFn(`请确认是否删除 ${row.organizationName}(${row.organizationCode}) 机构的亲访专员自动分派？`, '删除自动分派').then(res => {
        if (res) {
          api.deleteInvestigative(row.organizationId).then(res => {
            const { responseCode } = res
            if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
              this.getList(0)
              console.log('删除成功')
            }
          })
        }
      })
    },
    getUserData (data) {
      this.userValueList = [...data]
      const userIds = data.map(ele => ele.userId)
      if (!this.editInves) {
        api.editInvestigativeUser({
          organizationId: this.editUser.organizationId,
          userIdList: [...userIds]
        }).then(res => {
          this.successFn(res, 'editUserPop')
        })
      } else {
        this.editUserPop = false
      }
    },
    closeUserChoose (res) {
      this.editUserPop = res
    },
    getInvesData (data) {
      console.log(data, '调查机构提交参数')
      api.addInvestigative(data).then(res => {
        this.successFn(res, 'editInves')
      })
    },
    closeInvesChoose (val) {
      this.editInves = val
    },
    addInves () {
      this.editInves = true
      this.userValueList = []
    },
    showUser (type) {
      if (type === 'add') {
        this.editUserPop = true
      }
    },
    clearList (val) {
      this.userValueList = val
    },
    successFn (res, dialog) {
      const { responseCode, responseMsg: message } = res
      if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
        this.$message({
          message,
          type: 'success'
        })
        if (dialog) {
          this[dialog] = false
        }
        this.selectedIds = []
        this.getList(0)
      }
    },
    onReset () {
      this.dataForm = {
        freeCondition: '',
        userNameOrRealName: '',
        pageNum: 1,
        pageSize: 10
      }
    }
  }
}
</script>
